//
//  ViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "GetDataMethods.h"
#import "UIImageView+WebCache.h"
#import "SearchElements.h"
#import "RollPicture.h"
#import "AllData.h"
#import "TravelTableViewCell.h"
#import "TopicTableViewCell.h"
#import "SearchCollectionViewCell.h"
#import "TitleCollectionReusableView.h"
#import "SearchViewController.h"
#import "MBProgressHUD.h"

#import "RollPictureViewController.h"//滚动视图推出页
#import "AllButtonViewController.h"//全部按钮推出页
#import "AllPushViewController.h"//四个精选故事推出页
#import "DownDetailsViewController.h"//下部详情点击推出页
#import "ProductViewController.h"//下部详情推出UIWebView;
#import "OtherViewController.h"
#import "pinyin.h"
#import "Model.h"
#import "Fit.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UIPageControl *pageControl;
@property(nonatomic, retain)NSMutableArray *searchArray;
@property(nonatomic, retain)NSMutableArray *searchArr;
@property(nonatomic, retain)NSMutableArray *pictureArray;
@property(nonatomic, retain)NSMutableArray *dataArray;
@property(nonatomic, retain)NSMutableArray *eleArray;
@property(nonatomic, retain)UISearchBar *searchBar;
@property(nonatomic, retain)UIView *searchView;
@property(nonatomic, retain)UIButton *cancelButton;
@property(nonatomic, retain)UIButton *searchButton;
@property(nonatomic, assign)NSInteger flag;

@property(nonatomic, retain)NSString *spot_id1;
@property(nonatomic, retain)NSString *spot_id2;
@property(nonatomic, retain)NSString *spot_id3;
@property(nonatomic, retain)NSString *spot_id4;
@property(nonatomic, retain)NSString *urlString;
@property(nonatomic, retain)NSString *string;

@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation ViewController

-(void)dealloc
{
    [_tableView release];
    [_scrollView release];
    [_pageControl release];
    [_searchArray release];
    [_searchArr release];
    [_pictureArray release];
    [_dataArray release];
    [_eleArray release];
    [_searchBar release];
    [_searchView release];
    [_cancelButton release];
    [_searchButton release];
    [_collectionView release];
    [_progressView release];
    [super dealloc];
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    self.tabBarController.tabBar.barTintColor = [Model shareInstance].BackgroundColor;
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    self.navigationItem.titleView = searchView;
    [searchView release];
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(10 * W, 5, 260 * W, 30)];
    _searchBar.delegate = self;
    _searchBar.placeholder = @"搜索国家、城市、景区、游览胜地";
    _searchBar.layer.cornerRadius = 5;
    _searchBar.layer.masksToBounds = YES;
    _searchBar.backgroundColor = [UIColor clearColor];
    [searchView addSubview:_searchBar];
    [_searchBar release];
    
    self.searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _searchButton.frame = CGRectMake(_searchBar.frame.origin.x + _searchBar.frame.size.width + 10 * W, _searchBar.frame.origin.y, 40 * W, _searchBar.frame.size.height);
    [_searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [_searchButton addTarget:self action:@selector(searchButtonAction) forControlEvents:UIControlEventTouchUpInside];
    _searchButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_searchButton setTitleColor:[UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1] forState:UIControlStateNormal];
    [searchView addSubview:_searchButton];
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _cancelButton.frame = CGRectMake(_searchBar.frame.origin.x + _searchBar.frame.size.width + _searchButton.frame.size.width + 5 * W, _searchBar.frame.origin.y, _searchButton.frame.size.width, _searchButton.frame.size.height);
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [_cancelButton setHidden:YES];
    _cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_cancelButton setTitleColor:[UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1] forState:UIControlStateNormal];
    [searchView addSubview:_cancelButton];
    
    //上部推出页面
    self.searchView = [[UIView alloc]initWithFrame:CGRectMake(0, - self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _searchView.backgroundColor = [UIColor whiteColor];
    _flag = 0;
    [self.view addSubview:_searchView];
    [_searchView release];
    
    
    self.searchArray = [NSMutableArray array];
    self.searchArr = [NSMutableArray array];
    self.pictureArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    self.eleArray = [NSMutableArray array];
    [self changeColor];
    [self getData];
    
}

-(void)loadView
{
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //计时器
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(chageImage) userInfo:nil repeats:YES];
    
    //加载视图初始化
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
    
}

#pragma mark - tableView头视图(scrollview 和四个 精选故事)
-(void)createViews
{
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 760 * H)];
    _tableView.tableHeaderView = headView;
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * H)];
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * (_pictureArray.count + 2), 200 * H);
    _scrollView.contentOffset = CGPointMake(375 * W, 0);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    [headView addSubview:_scrollView];
    
    //滚动视图=========================
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375 * W, 200 * H)];
    [leftImageView sd_setImageWithURL:[NSURL URLWithString:[[_pictureArray lastObject] image_url]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    leftImageView.tag = 1000;
    [_scrollView addSubview:leftImageView];
    
    UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPictureAction:)];
    leftImageView.userInteractionEnabled = YES;
    [leftImageView addGestureRecognizer:leftTap];
    [leftTap release];

    for (NSInteger i = 0; i < _pictureArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_scrollView.frame.size.width * (i + 1), 0, 375 * W, 200 * H)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[_pictureArray[i] image_url]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
        imageView.tag = 1001 + i;
        [_scrollView addSubview:imageView];
        [imageView release];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPictureAction:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
        [tap release];
    }
    
    UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(375 * W * (_pictureArray.count + 1), 0, 375 * W, 200 * H)];
    rightImageView.tag = 1004;
    [rightImageView sd_setImageWithURL:[NSURL URLWithString:[[_pictureArray firstObject] image_url]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    [_scrollView addSubview:rightImageView];
    [rightImageView release];
    
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPictureAction:)];
    rightImageView.userInteractionEnabled = YES;
    [rightImageView addGestureRecognizer:rightTap];
    [rightTap release];
    
    //小白点
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(180 * W, 180 * H, 15 * W, 10 * H)];
    _pageControl.numberOfPages = _pictureArray.count;
    [headView addSubview:_pageControl];
    [_pageControl release];
    
    //==================================
    
    UIImageView *titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10 * W, 220 * H, 30 * W, 30 * H)];
    titleImageView.image = [UIImage imageNamed:@"cc"];
    [headView addSubview:titleImageView];
    [titleImageView release];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(titleImageView.frame.origin.x + titleImageView.frame.size.width, titleImageView.frame.origin.y, 265 * W, titleImageView.frame.size.height)];
    titleLabel.text = [[_eleArray[1]objectForKey:@"data"][0]objectForKey:@"title"];
    titleLabel.textColor = [UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1];
    [headView addSubview:titleLabel];
    [titleLabel release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width, titleLabel.frame.origin.y + 5 * H, 60 * W, 20 * H);
    [button setTitle:@"全部" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(allButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:button];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40 * W, 0, 20 * W, button.frame.size.height)];
    imageView.image = [UIImage imageNamed:@"xiangyou"];
    [button addSubview:imageView];
    [imageView release];

    UIImageView *backImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10 * W, titleLabel.frame.origin.y + titleLabel.frame.size.height + 10 * H, 170 * W, 220 * H)];
    backImageView1.layer.cornerRadius = 5;
    backImageView1.layer.masksToBounds = YES;
    backImageView1.tag = 1000;
    [headView addSubview:backImageView1];
    [backImageView1 release];
    UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(storyTapAction:)];
    backImageView1.userInteractionEnabled = YES;
    [backImageView1 addGestureRecognizer:firstTap];
    [firstTap release];
    UIImageView *pictureImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, backImageView1.frame.size.width, 120 * H)];
    [pictureImageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_eleArray[2] objectForKey:@"data"][0]objectForKey:@"index_cover"]]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    [backImageView1 addSubview:pictureImageView1];
    [pictureImageView1 release];
    UILabel *indexLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(pictureImageView1.frame.origin.x + 10 * W, pictureImageView1.frame.origin.y + pictureImageView1.frame.size.height + 10 * H, pictureImageView1.frame.size.width - 20 * W, 40 * H)];
    indexLabel1.text = [[_eleArray[2] objectForKey:@"data"][0]objectForKey:@"index_title"];
    indexLabel1.font = [UIFont systemFontOfSize:15];
    indexLabel1.numberOfLines = 0;
    [backImageView1 addSubview:indexLabel1];
    [indexLabel1 release];
    UIImageView *userImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(indexLabel1.frame.origin.x, indexLabel1.frame.origin.y + indexLabel1.frame.size.height + 10 * H, 30 * W, 30 * W)];
    [userImageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[[_eleArray[2] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"avatar_m"]]]placeholderImage:[UIImage imageNamed:@"dengdai"]];
    userImageView1.layer.cornerRadius = userImageView1.frame.size.width / 2;
    userImageView1.layer.masksToBounds = YES;
    [backImageView1 addSubview:userImageView1];
    [userImageView1 release];
    UILabel *userLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(userImageView1.frame.origin.x + userImageView1.frame.size.width + 10 * W, userImageView1.frame.origin.y + 5 * H, indexLabel1.frame.size.width - userImageView1.frame.size.width - 10 * W, 20 * H)];
    userLabel1.text = [[[_eleArray[2] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"name"];
    userLabel1.font = [UIFont systemFontOfSize:13];
    [backImageView1 addSubview:userLabel1];
    [userLabel1 release];
    
    UIImageView *backImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(backImageView1.frame.origin.x + backImageView1.frame.size.width + 15 * W, backImageView1.frame.origin.y, backImageView1.frame.size.width, backImageView1.frame.size.height)];
    backImageView2.layer.cornerRadius = 5;
    backImageView2.layer.masksToBounds = YES;
    backImageView2.tag = 1001;
    [headView addSubview:backImageView2];
    [backImageView2 release];
    UITapGestureRecognizer *secondTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(storyTapAction:)];
    backImageView2.userInteractionEnabled = YES;
    [backImageView2 addGestureRecognizer:secondTap];
    [secondTap release];
    UIImageView *pictureImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, backImageView2.frame.size.width, 120 * H)];
    [pictureImageView2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_eleArray[3] objectForKey:@"data"][0]objectForKey:@"index_cover"]]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    [backImageView2 addSubview:pictureImageView2];
    [pictureImageView2 release];
    UILabel *indexLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(pictureImageView2.frame.origin.x + 10 * W, pictureImageView2.frame.origin.y + pictureImageView2.frame.size.height + 10 * H, pictureImageView2.frame.size.width - 20 * W, 40 * H)];
    indexLabel2.text = [[_eleArray[3] objectForKey:@"data"][0]objectForKey:@"index_title"];
    indexLabel2.font = [UIFont systemFontOfSize:15];
    indexLabel2.numberOfLines = 0;
    [backImageView2 addSubview:indexLabel2];
    [indexLabel2 release];
    UIImageView *userImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(indexLabel2.frame.origin.x, indexLabel2.frame.origin.y + indexLabel2.frame.size.height + 10 * H, 30 * W, 30 * W)];
    [userImageView2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[[_eleArray[3] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"avatar_m"]]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    userImageView2.layer.cornerRadius = userImageView2.frame.size.width / 2;
    userImageView2.layer.masksToBounds = YES;
    [backImageView2 addSubview:userImageView2];
    [userImageView2 release];
    UILabel *userLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(userImageView2.frame.origin.x + userImageView2.frame.size.width + 10 * W, userImageView2.frame.origin.y + 5 * H, indexLabel2.frame.size.width - userImageView2.frame.size.width - 10 * W, 20 * H)];
    userLabel2.text = [[[_eleArray[3] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"name"];
    userLabel2.font = [UIFont systemFontOfSize:13];
    [backImageView2 addSubview:userLabel2];
    [userLabel2 release];
    
    UIImageView *backImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(10 * W, backImageView1.frame.origin.y + backImageView1.frame.size.height + 10 * H, backImageView1.frame.size.width, backImageView1.frame.size.height)];
    backImageView3.layer.cornerRadius = 5;
    backImageView3.layer.masksToBounds = YES;
    backImageView3.tag = 1002;
    [headView addSubview:backImageView3];
    [backImageView3 release];
    UITapGestureRecognizer *thirdTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(storyTapAction:)];
    backImageView3.userInteractionEnabled = YES;
    [backImageView3 addGestureRecognizer:thirdTap];
    [thirdTap release];
    UIImageView *pictureImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, backImageView3.frame.size.width, 120 * H)];
    [pictureImageView3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_eleArray[4] objectForKey:@"data"][0]objectForKey:@"index_cover"]]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    [backImageView3 addSubview:pictureImageView3];
    [pictureImageView3 release];
    UILabel *indexLabel3 = [[UILabel alloc]initWithFrame:CGRectMake(pictureImageView3.frame.origin.x + 10 * W, pictureImageView3.frame.origin.y + pictureImageView3.frame.size.height + 10 * H, pictureImageView3.frame.size.width - 20 * W, 40 * H)];
    indexLabel3.text = [[_eleArray[4] objectForKey:@"data"][0]objectForKey:@"index_title"];
    indexLabel3.font = [UIFont systemFontOfSize:15];
    indexLabel3.numberOfLines = 0;
    [backImageView3 addSubview:indexLabel3];
    [indexLabel3 release];
    UIImageView *userImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(indexLabel3.frame.origin.x, indexLabel3.frame.origin.y + indexLabel3.frame.size.height + 10 * H, 30 * W, 30 * W)];
    [userImageView3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[[_eleArray[4] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"avatar_m"]]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    userImageView3.layer.cornerRadius = userImageView3.frame.size.width / 2;
    userImageView3.layer.masksToBounds = YES;
    [backImageView3 addSubview:userImageView3];
    [userImageView3 release];
    UILabel *userLabel3 = [[UILabel alloc]initWithFrame:CGRectMake(userImageView3.frame.origin.x + userImageView3.frame.size.width + 10 * W, userImageView3.frame.origin.y + 5 * H, indexLabel3.frame.size.width - userImageView3.frame.size.width - 10 * W, 20 * H)];
    userLabel3.text = [[[_eleArray[4] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"name"];
    userLabel3.font = [UIFont systemFontOfSize:13];
    [backImageView3 addSubview:userLabel3];
    [userLabel3 release];
    
    UIImageView *backImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(backImageView2.frame.origin.x, backImageView3.frame.origin.y, backImageView1.frame.size.width, backImageView1.frame.size.height)];
    backImageView4.layer.cornerRadius = 5;
    backImageView4.layer.masksToBounds = YES;
    backImageView4.tag = 1003;
    [headView addSubview:backImageView4];
    [backImageView4 release];
    UITapGestureRecognizer *fourthTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(storyTapAction:)];
    backImageView4.userInteractionEnabled = YES;
    [backImageView4 addGestureRecognizer:fourthTap];
    [fourthTap release];
    UIImageView *pictureImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, backImageView4.frame.size.width, 120 * H)];
    [pictureImageView4 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[_eleArray[5] objectForKey:@"data"][0]objectForKey:@"index_cover"]]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    [backImageView4 addSubview:pictureImageView4];
    [pictureImageView4 release];
    UILabel *indexLabel4 = [[UILabel alloc]initWithFrame:CGRectMake(pictureImageView4.frame.origin.x + 10 * W, pictureImageView4.frame.origin.y + pictureImageView4.frame.size.height + 10 * H, pictureImageView4.frame.size.width - 20 * W, 40 * H)];
    indexLabel4.text = [[_eleArray[5] objectForKey:@"data"][0]objectForKey:@"index_title"];
    indexLabel4.font = [UIFont systemFontOfSize:15];
    indexLabel4.numberOfLines = 0;
    [backImageView4 addSubview:indexLabel4];
    [indexLabel4 release];
    UIImageView *userImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(indexLabel4.frame.origin.x, indexLabel4.frame.origin.y + indexLabel4.frame.size.height + 10 * H, 30 * W, 30 * W)];
    [userImageView4 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [[[_eleArray[5] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"avatar_m"]]] placeholderImage:[UIImage imageNamed:@"dengdai"]];
    userImageView4.layer.cornerRadius = userImageView4.frame.size.width / 2;
    userImageView4.layer.masksToBounds = YES;
    [backImageView4 addSubview:userImageView4];
    [userImageView4 release];
    UILabel *userLabel4 = [[UILabel alloc]initWithFrame:CGRectMake(userImageView4.frame.origin.x + userImageView4.frame.size.width + 10 * W, userImageView4.frame.origin.y + 5 * H, indexLabel4.frame.size.width - userImageView4.frame.size.width - 10 * W, 20 * H)];
    userLabel4.text = [[[_eleArray[5] objectForKey:@"data"][0]objectForKey:@"user"]objectForKey:@"name"];
    userLabel4.font = [UIFont systemFontOfSize:14];
    [backImageView4 addSubview:userLabel4];
    [userLabel4 release];
    
    UIImageView *titleImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(backImageView3.frame.origin.x, backImageView3.frame.origin.y + backImageView3.frame.size.height + 10 * H, titleImageView.frame.size.width, titleImageView.frame.size.height)];
    titleImageView2.image = [UIImage imageNamed:@"cc"];
    [headView addSubview:titleImageView2];
    [titleImageView2 release];
    UILabel *noteLabel = [[UILabel alloc]initWithFrame:CGRectMake(titleImageView2.frame.origin.x + titleImageView2.frame.size.width, titleImageView2.frame.origin.y, titleLabel.frame.size.width, titleLabel.frame.size.height)];
    noteLabel.text = [[_eleArray[6]objectForKey:@"data"][0]objectForKey:@"title"];
    noteLabel.textColor = [UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1];
    [headView addSubview:noteLabel];
    [noteLabel release];
    
    backImageView1.backgroundColor = [UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1];
    backImageView2.backgroundColor = [UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1];
    backImageView3.backgroundColor = [UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1];
    backImageView4.backgroundColor = [UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1];
    indexLabel1.textColor = [UIColor whiteColor];
    indexLabel2.textColor = [UIColor whiteColor];
    indexLabel3.textColor = [UIColor whiteColor];
    indexLabel4.textColor = [UIColor whiteColor];
    userLabel1.textColor = [UIColor whiteColor];
    userLabel2.textColor = [UIColor whiteColor];
    userLabel3.textColor = [UIColor whiteColor];
    userLabel4.textColor = [UIColor whiteColor];
    
    _spot_id1 = [[_eleArray[2]objectForKey:@"data"][0] objectForKey:@"spot_id"];
    _spot_id2 = [[_eleArray[3]objectForKey:@"data"][0] objectForKey:@"spot_id"];
    _spot_id3 = [[_eleArray[4]objectForKey:@"data"][0] objectForKey:@"spot_id"];
    _spot_id4 = [[_eleArray[5]objectForKey:@"data"][0] objectForKey:@"spot_id"];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(105 * W, 30 * H);
    flowLayout.minimumLineSpacing = 15 * W;
    flowLayout.minimumInteritemSpacing = 15 * H;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * H, 15 * W, 10 * H, 15 * W);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375 * W, 667 * H) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [_searchView addSubview:_collectionView];
    [_collectionView release];
    [_collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_collectionView registerClass:[TitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
}
#pragma mark - 数据解析
-(void)getData
{
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/index/"] BodyString:nil WithDataBlock:^(id data) {
        
        //搜索数据
        NSArray *searchArray = [[data objectForKey:@"data"] objectForKey:@"search_data"];
        for (NSDictionary *searchDic in searchArray) {
            NSArray *elementsArray = [searchDic objectForKey:@"elements"];
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *elementsDic in elementsArray) {
                SearchElements *search = [[SearchElements alloc]init];
                [search setValuesForKeysWithDictionary:elementsDic];
                [array addObject:search];
            }
            [_searchArr addObject:[searchDic objectForKey:@"title"]];
            [_searchArray addObject:array];
        }
        //滚动视图和详情
        _eleArray = [[data objectForKey:@"data"] objectForKey:@"elements"];
        for (NSDictionary *eleDic in _eleArray) {
            if ([[eleDic objectForKey:@"type"] integerValue] == 1) {
                NSDictionary *pictureDic = _eleArray[0];
                NSArray *pictureArray = [pictureDic objectForKey:@"data"];
                for (NSDictionary *picDic in pictureArray[0]) {
                    RollPicture *picture = [[RollPicture alloc]init];
                    [picture setValuesForKeysWithDictionary:picDic];
                    [_pictureArray addObject:picture];
                }
            }
            else if ([[eleDic objectForKey:@"type"] integerValue] != 1 && [[eleDic objectForKey:@"type"] integerValue] != 11 && [[eleDic objectForKey:@"type"] integerValue] != 10 && [[eleDic objectForKey:@"type"] integerValue] != 9) {
                NSArray *dataArr = [eleDic objectForKey:@"data"];
                for (NSDictionary *dataDic in dataArr) {
                    AllData *data = [[AllData alloc]init];
                    [data setValuesForKeysWithDictionary:dataDic];
                    [_dataArray addObject:data];
                }
            }
        }
        [_tableView reloadData];
        [self createViews];
        [_progressView removeFromSuperview];
    }];
}
#pragma mark - tableview的cell的样式,高度,数量
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据是否有user, 判断使用哪一种cell;
    if ([_dataArray[indexPath.row] user]) {
        static NSString *identifier = @"cell";
        TravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[TravelTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        //数据保护
        if (_dataArray.count != 0) {
            AllData *allData = _dataArray[indexPath.row];
            cell.allData = allData;
        }
        cell.backgroundColor = [Model shareInstance].BackgroundColor;
        return cell;
    }
    static NSString *identifier1 = @"cell1";
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
    if (!cell) {
        cell = [[TopicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
    }
    //数据保护
    if (_dataArray.count != 0) {
        AllData *allData = _dataArray[indexPath.row];
        cell.allData = allData;
    }
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200 * H;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
#pragma mark - tableview点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //这里需要判断
    if ([_dataArray[indexPath.row] user]) {
        DownDetailsViewController *downVC = [[DownDetailsViewController alloc]init];
        downVC.string = [[_dataArray[indexPath.row] Id] copy];
        [self.navigationController pushViewController:downVC animated:YES];
    }
    else{
        ProductViewController *productVC = [[ProductViewController alloc]init];
        productVC.string = (NSString *)[_dataArray[indexPath.row] url];
        [self.navigationController pushViewController:productVC animated:YES];
    }
    
}

#pragma mark - scrollview 循环滚动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage = _scrollView.contentOffset.x / (375 * W) - 1;
    
    if (_scrollView.contentOffset.x >= (_pictureArray.count + 1) * (375 * W)) {
        
        _pageControl.currentPage = 0;
        _scrollView.contentOffset = CGPointMake(375 * W, 0);
        
    }else if(_scrollView.contentOffset.x < 375 * W){
        _pageControl.currentPage = _pictureArray.count;
        _scrollView.contentOffset = CGPointMake(_pictureArray.count * (375 * W), 0);
    }
}
#pragma mark - 计时器方法
-(void)chageImage
{
    [UIView animateWithDuration:0.5 animations:^{
        [_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x + self.view.frame.size.width, 0)];
    }];
    _pageControl.currentPage = _scrollView.contentOffset.x / (375 * W) - 1;
    if (_scrollView.contentOffset.x > _pictureArray.count * _scrollView.frame.size.width) {
        _scrollView.contentOffset = CGPointMake(375 * W, 0);
        _pageControl.currentPage = 0;//小白点回到0点
    }
}
#pragma mark - 滚动视图轻拍手势, 通过tag值跳转到对应的webView
-(void)tapPictureAction:(UITapGestureRecognizer *)tap
{
    RollPictureViewController *rollVC = [[RollPictureViewController alloc]init];
    NSInteger i = tap.view.tag - 1000;
    if (i == 0) {
        rollVC.roll = (NSString *)[[_pictureArray lastObject]  html_url];
    }
    else if (i == _pictureArray.count + 1) {
        rollVC.roll = (NSString *)[[_pictureArray firstObject] html_url];
    }
    else {
        rollVC.roll = (NSString *)[_pictureArray [i - 1] html_url  ];
    }
    [self.navigationController pushViewController:rollVC animated:YES];
}
#pragma mark - 按钮"全部"点击方法
-(void)allButtonAction
{
    AllButtonViewController *allVC = [[AllButtonViewController alloc]init];
    [self.navigationController pushViewController:allVC animated:YES];
}
#pragma mark - 四个精选故事点击事件
-(void)storyTapAction:(UITapGestureRecognizer *)tap
{
    AllPushViewController * pushVC = [[AllPushViewController alloc]init];
    NSInteger i = tap.view.tag - 1000;
   
    if (i == 0) {
        pushVC.spot_id = _spot_id1;
    }
    else if (i == 1) {
        pushVC.spot_id = _spot_id2;
    }
    else if (i == 2) {
        pushVC.spot_id = _spot_id3;
    }
    else {
        pushVC.spot_id = _spot_id4;
    }
    [self.navigationController pushViewController:pushVC animated:YES];
}
#pragma mark - collectionview cell 样式
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
        cell.label.text = [_searchArray[0][indexPath.row] name];
        return cell;
    }
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    cell.label.text = [_searchArray[1][indexPath.row] name];
    return cell;
}
#pragma mark - collectionview item 个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_searchArray.count == 0) {
        return 0;
    }
    else{
    if (section == 0) {
        return [_searchArray[0] count];
    }
    return [_searchArray[1] count];
    }
}
#pragma mark - collectionview section 个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _searchArray.count;
}
#pragma mark - collectionview section headview的大小
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size = {320 * W, 50 * H};
    return size;
}
#pragma mark - 为collection view添加一个补充视图(页眉或页脚)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    TitleCollectionReusableView *headView = [[TitleCollectionReusableView alloc]init];
    headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    
    headView.titleLabel.text = [NSString stringWithFormat:@"%@", _searchArr[indexPath.section]];
    headView.titleLabel.textColor = [UIColor colorWithRed:0.7 green:0.34 blue:0.65 alpha:1];
    headView.iconImageView.image = [UIImage imageNamed:@"ee"];
    return headView;
}
#pragma mark - collectionview 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    OtherViewController *otherVC = [[OtherViewController alloc]init];
    if (_searchArray.count != 0) {
        otherVC.data = _searchArray[indexPath.section][indexPath.row];
    }
    [self.navigationController pushViewController:otherVC animated:YES];
}

#pragma mark - searchBar 点击推出界面
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    if(_flag == 1){
        [UIView animateWithDuration:.5 animations:^{
            _searchView.frame = CGRectMake(0, - 667 * H, 375 * W, 667 * H);
        }];
        _flag = 0;
    }else
    {
        [UIView animateWithDuration:.5 animations:^{
            _searchView.frame = CGRectMake(0, 0, 375 * W, 667 * H);
            [_cancelButton setHidden:NO];

        }];
        _flag = 1;
    }
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    if(_flag == 0){
        [UIView animateWithDuration:.5 animations:^{
            _searchView.frame = CGRectMake(0, 0, 375 * W, 667 * H);
        }];
        _flag = 1;
    }else
    {
        [UIView animateWithDuration:.5 animations:^{
            _searchView.frame = CGRectMake(0, - 667 * H, 375 * W, 667 * H);
            [_cancelButton setHidden:YES];
        }];
        _flag = 0;
    }
}
-(void)cancelButtonAction
{
    [_searchBar resignFirstResponder];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.urlString = searchText;
}
#pragma mark - searchButton 点击方法
-(void)searchButtonAction
{
    if (_urlString.length == 0) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"请输入搜索内容"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    SearchViewController *searchVC = [[SearchViewController alloc]init];
    searchVC.urlString = _urlString;
    [self.navigationController pushViewController:searchVC animated:YES];
    [_searchBar resignFirstResponder];
}

#pragma mark - 键盘搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{

    SearchViewController *searchVC = [[SearchViewController alloc]init];
    searchVC.urlString = _urlString;
    [self.navigationController pushViewController:searchVC animated:YES];
    [_searchBar resignFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [UIView animateWithDuration:.5 animations:^{
        _searchView.frame = CGRectMake(0, - 667 * H, 375 * W, 667 * W);
        [_cancelButton setHidden:YES];
    }];
    [_searchBar resignFirstResponder];
}


















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
